package com.turing.post.tuxedo.service;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.transaction.util.MrchBusiness;
import com.turing.post.transaction.util.MrchTransParam;
import com.turing.post.tuxedo.util.IPaidCorr;
import com.turing.post.tuxedo.util.LocalService;
import com.turing.post.tuxedo.util.LocalServiceImpl;
import com.turing.post.tuxedo.util.OPaidCorr;
import com.turing.post.tuxedo.util.PCPSService;
import com.turing.post.tuxedo.util.TransRequest;
import com.turing.post.tuxedo.util.TransResponse;
import com.turing.post.tuxedo.util.TuxedoRespCode;
import com.turing.post.util.PCPSApplicationException;

@Stateless(mappedName = "PCPS.SVC9011")
@Remote( { LocalService.class, PCPSService.class })
@Local
public class PaidCorr extends LocalServiceImpl implements LocalService,
		PCPSService {

	@EJB(mappedName = "transaction.impl.MrchBusinessImpl")
	private MrchBusiness mrchBusinessImpl;

	/**
	 * generate IPaidCorr
	 * 
	 * @return IPaidCorr
	 */
	public IPaidCorr makeRequestObject() {
		return new IPaidCorr();
	}

	/**
	 * PaidCorr 数据检查
	 * 
	 * @param req
	 * @param ack
	 */
	protected void preService(TransRequest req, TransResponse ack)
			throws PCPSApplicationException {
	}

	/**
	 * PaidCorr service entry 预付卡支付冲正的业务逻辑
	 * 
	 * @param req
	 * @param ack
	 */
	protected void Service(TransRequest req, TransResponse ack)
			throws PCPSApplicationException {

		IPaidCorr itr = (IPaidCorr) req;
		OPaidCorr otr = (OPaidCorr) ack;

		// 设置基本参数
		MrchTransParam imtp = setMrchTransParam(req);
		// 被冲正交易流水
		imtp.cancelMrchReqSeqNo = req.reqSeqNo;
		// 交易摘要(可选)
		imtp.mrchAbstract = "冲正";

		// 代理操作员号
		imtp.OperId = null;

		// 合计发生额
		imtp.totalAmount = itr.totalAmount.add(itr.scorePaidAmount);

		// 执行交易
		MrchTransParam omtp = mrchBusinessImpl.PaidCorrect(imtp);

		// 主机流水号
		otr.ackSeqNo = omtp.tranSeqNo;
		otr.tranTime = itr.curSysDateTime;
		otr.respCode = TuxedoRespCode.RC_SUCCESS;
	}

	/**
	 * PaidCorr 服务入口
	 * 
	 * @param req
	 * @return TransResponse
	 */
	public TransResponse Service(TransRequest req)
			throws PCPSApplicationException {
		// 按普通方法执行服务
		return CommonService(req, new OPaidCorr());
	}
}